package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Menu;
import cn.itsource.system.domain.Permission;
import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.RoleMapper;
import cn.itsource.system.query.RoleQuery;
import cn.itsource.system.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：角色Role
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    public Role findOne(Long id) {
        return roleMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(Role role) {
        //添加角色信息
        roleMapper.add(role);
        //添加当前角色的权限和菜单
        if(role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role);
        }
        if(role.getMenus().size()>0){
            roleMapper.addRoleMenus(role);
        }
    }

    @Transactional
    @Override
    public void update(Role role) {
        //修改角色信息
        roleMapper.update(role);

        //删除角色的权限和菜单信息
        roleMapper.deleteRolePermissions(role.getId());
        roleMapper.deleteRoleMenus(role.getId());

        //添加当前角色的权限和菜单
        if(role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role);
        }
        if(role.getMenus().size()>0){
            roleMapper.addRoleMenus(role);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
        //删除角色的权限和菜单信息
        roleMapper.deleteRolePermissions(id);
        roleMapper.deleteRoleMenus(id);
        //删除员工信息
        roleMapper.delete(id);
    }

    @Transactional
    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public PageInfo<Role> queryPage(RoleQuery roleQuery) {
        //开启分页
        PageHelper.startPage(roleQuery.getCurrentPage(),roleQuery.getPageSize());
        //1.首先会查询总条数
        //2.会根据方言或驱动名进行分页查询数据 - mysql - limit
        //3.会自动计算limit的第一个参数

        List<Role> list = roleMapper.queryPage(roleQuery);
        for (Role role : list) {
            //查询当前角色的所有权限
            List<Permission> dbPermissions = roleMapper.getPermissionsByRoleId(role.getId());
            //查询当前角色的所有菜单
            List<Menu> dbMenus = roleMapper.getMenusByRoleId(role.getId());
            role.setDbPermissions(dbPermissions);
            role.setDbMenus(dbMenus);
        }

        //调用PageInfo的有参会计算其他参数
        PageInfo<Role> pageInfo = new PageInfo<Role>(list);
        return pageInfo;
    }

}
